package com.dx.chat.server;

import com.dx.chat.util.AlertUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.BorderPane;
import javafx.scene.text.Text;
import lombok.extern.slf4j.Slf4j;

import java.net.InetAddress;
import java.net.URL;
import java.net.UnknownHostException;
import java.util.ResourceBundle;

/**
 * Description: 服务端控制类
 *
 * @author rockstarsteve
 * @version 1.0
 * @copyright Copyright (c) 电信
 * @since 2023/9/8
 */
@Slf4j
public class ServerController implements Initializable {

    private ServerInfoBean serverInfoBean = new ServerInfoBean();

    private ServerService serverService = new ServerService();

    @FXML
    private Text ip;
    @FXML
    private TextField port;
    @FXML
    private TextArea chatInfo;
    @FXML
    private TextField filePath;
    @FXML
    private TextField sendMsg;
    @FXML
    private BorderPane body;

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        chatInfo.setEditable(false);
        InetAddress address = null;
        try {
            address = InetAddress.getLocalHost();
        } catch (UnknownHostException e) {
            log.error("获取本机ip失败！");
            AlertUtil.alertWarn("提示", "获取本机ip失败");
        }
        //绑定内容
        ip.textProperty().bindBidirectional(serverInfoBean.ipProperty());
        port.textProperty().bindBidirectional(serverInfoBean.portProperty());
        chatInfo.textProperty().bindBidirectional(serverInfoBean.chatInfoStringProperty());
        filePath.textProperty().bindBidirectional(serverInfoBean.filePathProperty());
        sendMsg.textProperty().bindBidirectional(serverInfoBean.sendMsgProperty());
        serverInfoBean.setIp(address.getHostAddress());
    }

    public void startServer(ActionEvent actionEvent) {
        serverService.startServer(serverInfoBean);
    }

    public void sendFile(ActionEvent actionEvent) {
        serverService.sendFile(serverInfoBean);
    }

    public void sendMsg(ActionEvent actionEvent) {
        serverService.sendMsg(serverInfoBean);
    }

    public void chooseFile(MouseEvent mouseEvent) {
        serverService.chooseFile(serverInfoBean, body);
    }

}
